/*
 * Copyright (c) 2014, 2015, XIANDIAN and/or its affiliates. All rights reserved.
 * XIANDIAN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package com.xd.cloud.dao.core;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.xd.cloud.dao.BaseDao;
import com.xd.cloud.dao.Page;
import com.xd.cloud.entity.core.User;

/**
 * 用户Dao
 * 
 * @author 云计算应用与开发项目组
 * @since V1.0
 * 
 */
@Repository
public class UserDao extends BaseDao<User> {
	
	public List<User> getAll() {
		String hql = "from User";
		return find(hql);
	}
	
	//这儿处理有问题，先简单做下屏蔽处理
//	public Page getAllUser(int pageNo,int pageSize)
//	{
//		String hql = "from User where id != 0 order by id desc";
//		return pagedQuery(hql, pageNo, pageSize);
//	}
	
	public Page getAllUser(int pageNo, int pageSize) {
		String sql = "select a.id,a.account,a.name,a.updatetime,a.status,b.orgname,b.orgid from sys_user as a left join (select aa.userid,bb.name as orgname,bb.id as orgid from sys_user_company as aa left join sys_company as bb on aa.companyid = bb.id) as b on a.id=b.userid where a.id != 1 order by a.createtime desc";
		return pagedSqlQuery(sql, pageNo,pageSize);
	}
	
	public List<User> searchUser(String name,int orgid){
		
		name="%"+name+"%";
		String hql = "from User u where (u.username like ? or u.realname like ?) and u.id not in (select ou.user.id from OrgUser ou where ou.org.id=?)";
		return find(hql, new Object[]{name,name,orgid});
	}
	
	public List<User> search(String name){
		name="%"+name+"%";
		String hql = "from User u where (u.username like ? or u.realname like ?) order by id desc";
		return find(hql, new Object[]{name,name});
	}
	
	public List searchUser(String name){
		name="%"+name+"%";
		String sql = "select username,isenable,joindate,realname,email,id   from t_user  where ( username like ? or realname like ? ) order by id desc";
		return findSql(sql, new Object[]{name,name});
	}

	public Page getUsersByOrgId(int orgid, int pageNo, int pageSize) {
		Page pagedSqlQuery = pagedSqlQuery("select a.id,a.username,a.realname from t_user as a LEFT JOIN t_orguser as b on a.id=b.userid LEFT JOIN t_userrole as c on a.id=c.userid  where c.roleid=3 and b.orgid=?", pageNo, pageSize, new Object[]{orgid});
		return pagedSqlQuery;
	}	
	
	public User getByUserAccount(String username) {
		String hql = "from User where account = ?";
		return get(hql, new Object[]{username});
	}
	
	public Page geByOrgId(int pageNo, int pageSize,String orgids,String roleids) {
		String hql = "from User where id != 1 and id in (select user.id from OrgUser where org.id in ("+orgids+")) " +
					"and id in (select userid from UserRole where role.id in ("+roleids+")) order by id desc";
		return pagedQuery(hql, pageNo, pageSize);
	}
	
	public Page getByParmars(int pageNo, int pageSize,String orgids,String name,String joindate,Integer majorid,Integer classid,Integer isrecommend)
	{
		List<Object> values=new ArrayList<Object>();
		String hql ="from User where org.id in ("+orgids+") ";
		if(name!=null){
			name="%"+name+"%";
			hql+="and (username like ? or realname like ?) ";
			values.add(name);
			values.add(name);
		}
		if(joindate!=null){
			hql+="and joindate=? ";
			values.add(joindate);
		}
		if(majorid!=null){
			hql+="and id in (select user.id from ClassUser where classes.major.id=?) ";
			values.add(majorid);
		}
		if(classid!=null){
			hql+="and id in (select user.id from ClassUser where classes.id=?)";
			values.add(classid);
		}
		if(isrecommend!=null){
			hql+="and isrecommend=? ";
			values.add(isrecommend);
		}
		hql+="and id not in (select userid from UserRole where role.id=1 or role.id=2 or role.id=3) order by id desc";
		return pagedQuery(hql, pageNo, pageSize,values);
	}
	

	public void remove(String ids) {
		String hql = "delete from User where id in ("+ids+")";
		executeHql(hql);
	}
	
	public void resetpsw(String ids,String psw) {
		String hql = "update from User set password =? where id in ("+ids+")";
		executeHql(hql,new Object[]{psw});
	}
	
	public void updateisenable(String ids,int isenable) {
		String hql = "update from User set isenable =? where id in ("+ids+")";
		executeHql(hql,new Object[]{isenable});
	}
	
	public Page getByOrgidRoleid(int pageNo,int pageSize,String orgids,int roleid,String name){
		name="%"+name+"%";
		String hql = "from User where id != 1 and org.id in ("+orgids+") and "
					+ "(username like ? or realname like ? or joindate like ? or org.name like ? or email like ? or phone like ? or qq like ? or province like ? or city like ?) "
					+ "and id in (select userid from UserRole where role.id=?) order by id desc";
		return pagedQuery(hql, pageNo, pageSize,name,name,name,name,name,name,name,name,name,roleid);
	}
	

	public List<User> getByRolekey(int orgid,String  rolekeys)
	{
		String hql = "from User where org.id=? and id in (select userid from UserRole where role.rolekey in ("+rolekeys+")) order by username,realname";
    	return find(hql,new Object[]{orgid});
	}
	
	public List<User> getByOrgids(String orgids,int roleid)
	{
		String hql = "from User where org.id in ("+orgids+") and id in (select userid from UserRole where role.id=?) order by username,realname";
    	return find(hql,new Object[]{roleid});
	}
	
	public Object getCount(int orgid,int roleid)
	{
		String hql = "select count(*) from User where org.id=? and id in (select userid from UserRole where role.id=?)";
    	return getResult(hql,new Object[]{orgid,roleid});
	}
	
	public Object getCount(int roleid)
	{
		String hql = "select count(*) from User where id in (select userid from UserRole where role.id=?)";
    	return getResult(hql,new Object[]{roleid});
	}
	
	public Object getCountNew(int roleids)
	{
		String hql = "select count(*) from User where id in (select userid from UserRole where role.id in ?)";
    	return getResult(hql,new Object[]{roleids});
	}
	
	public Object getCountNewId(String roleids)
	{
		String hql = "select count(*) from User where id in (select userid from UserRole where role.id in ?)";
    	return find(hql, new Object[]{roleids});
	}
	
	
	
	public List<User> getByOrgids(String orgids)
	{
		String hql = "from User where org.id in ("+orgids+") order by username,realname";
    	return find(hql);
	}
	
	public Page getByParmars(int pageNo, int pageSize,int orgid,String joindate,int majorid)
	{
		List<Object> values=new ArrayList<Object>();
		String hql ="from User where 1=1 ";
		if (orgid!=-1) {
			hql+="and org.id=? ";
			values.add(orgid);
		}
		if(!"null".equals(joindate)){
			hql+="and joindate=? ";
			values.add(joindate);
		}
		if(majorid!=-1){
			hql+="and major.id=? ";
			values.add(majorid);
		}
	
		hql+="order by id desc";
		return pagedQuery(hql, pageNo, pageSize,values);
	}
	
	public Page getByClassid(int pageNo,int pageSize,int classid){
		String hql = "from User where id in (select user.id from ClassUser where classes.id=?) order by username";
		return pagedQuery(hql, pageNo, pageSize,classid);
	}
	
	public User getByUseridRolename(int userid,String rolekey){
		String hql = "from User where id=(select userid from UserRole where role.rolekey=?) order by id";
		return get(hql, new Object[]{userid,rolekey});
	}

    public Page getAllUserBySessionUserId(int pageNo, int pageSize, int sessionUserId) {
		String sql = "select a.id,a.account,a.name,a.updatetime,a.status,b.orgname,b.orgid,role.name as rolename from sys_user as a left join (select aa.userid,bb.name as orgname,bb.id as orgid from sys_user_company as aa left join sys_company as bb on aa.companyid = bb.id) as b on a.id=b.userid LEFT JOIN sys_userrole as userrole ON a.id = userrole.userid LEFT JOIN sys_role as role ON userrole.roleid = role.id where a.parentid="+sessionUserId+" and a.id != 1 order by a.createtime desc";
		return pagedSqlQuery(sql, pageNo,pageSize);
    }

//	public User getUserByPhone(String phone) {
//		String hql = "from User where phone = ?";
//		return get(hql, new Object[]{phone});
//	}
}